"use strict";function _typeof(t){return(_typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t})(t)}function IntentIqObject(t){this.version=4.01,this.intentIqData={},this.wasServerCalled=!1,this.wasCallFromConstructor=!1,this.wasCallbackFired=!1,this.wasDebugCheck=!1,this.isDebugMode=!1,this.callCount=0,this.noDataCounter=0,this.failCount=0,this.metadataConstant=256,this.syncRefreshMillis=36e5,this.shouldCallServer=!1,this.prebidDetectionRetries=100,this.prebidDellayBeforeRetry=20,this.pbjs=void 0,this.translateMetadata=function(t){try{var e=t.split(".");return((+e[0]*this.metadataConstant+ +e[1])*this.metadataConstant+ +e[2])*this.metadataConstant+ +e[3]}catch(t){return NaN}},this.vrOperationalMode="VR",this.pixelOperationalMode="PIXEL",this.dualOperationalMode="DUAL",this.getOperationalMode=function(t){try{switch(t.toLowerCase()){case"bidenhancement":return this.vrOperationalMode;case"sync":return this.pixelOperationalMode;case"both":return this.dualOperationalMode}}catch(t){this.logger(t)}return"DUAL"},this.verifyIdType=function(t){return 0===t||1===t||3===t||4===t?t:-1},this.intentIqConfig={partner:t?t.partner:null,refreshInMillis:t&&"number"==typeof t.refreshInMillis?t.refreshInMillis:432e5,timeoutInMillis:t&&"number"==typeof t.timeoutInMillis?t.timeoutInMillis:3e3,isAsyncServerRequest:!t||"boolean"!=typeof t.isAsyncServerRequest||t.isAsyncServerRequest,callback:t&&"function"==typeof t.callback?t.callback:null,enhanceRequests:!t||"boolean"!=typeof t.enhanceRequests||t.enhanceRequests,sourceMetaData:t&&"string"==typeof t.sourceMetaData?this.translateMetadata(t.sourceMetaData):this.translateMetadata(""),iiqServerAddress:t&&"string"==typeof t.iiqServerAddress?t.iiqServerAddress:"https://api.intentiq.com",iiqPixelServerAddress:t&&"string"==typeof t.iiqPixelServerAddress?t.iiqPixelServerAddress:"https://sync.intentiq.com",isCellular:!(!t||"boolean"!=typeof t.isCellular)&&t.isCellular,mode:t&&"string"==typeof t.mode?this.getOperationalMode(t.mode):"DUAL",partnerClientId:t&&"string"==typeof t.partnerClientId?t.partnerClientId:"",partnerClientIdType:t&&"number"==typeof t.partnerClientIdType?this.verifyIdType(t.partnerClientIdType):-1},this.currentDataTTl=this.intentIqConfig.refreshInMillis,this.intentIqSyncPartnerId=1048688155,this.FIRST_PARTY_KEY="_iiq_fdata",this.PARTNER_DATA_KEY=this.FIRST_PARTY_KEY+"_"+this.intentIqConfig.partner,this.LAST_SYNC_KEY="_iiq_sync";var e=this;this.logger=function(t){this.isDebug()&&console.log(t)}.bind(this),this.updateMetaData=function(t){this.intentIqConfig.sourceMetaData=this.translateMetadata(t)},this.generateGUID=function(){var i=(new Date).getTime();return"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,function(t){var e=(i+16*Math.random())%16|0;return i=Math.floor(i/16),("x"===t?e:3&e|8).toString(16)})},this.isDebug=function(){var t;return this.wasDebugCheck||(-1===(t=window.location.href).indexOf("?debug=true")&&-1===t.indexOf("&debug=")||(this.isDebugMode=!0),this.wasDebugCheck=!0),this.isDebugMode},this.hasLocalStorage=function(){try{return!!window.localStorage}catch(t){this.failCount++,this.logger("Local storage api disabled")}return!1},this.clearCounters=function(){this.callCount=0,this.failCount=0,this.noDataCounter=0},this.readData=function(t){try{if(this.hasLocalStorage())return window.localStorage.getItem(t)}catch(t){this.failCount++,this.logger(t)}return null},this.storeData=function(t,e){try{this.logger("IntentIQ: storing data: key="+t+" value="+e),e&&this.hasLocalStorage()&&window.localStorage.setItem(t,e)}catch(t){this.failCount++,this.logger(t)}},this.generateRandomUsIp=function(){var t=window.localStorage.getItem("_iiq_ip");return(!t||this.isIpCellular(t)&&!this.intentIqConfig.isCellular||!this.isIpCellular(t)&&this.intentIqConfig.isCellular)&&(t=this.generateNewRandomIp(this.intentIqConfig.isCellular),window.localStorage.setItem("_iiq_ip",t)),t},this.isIpCellular=function(t){return t.startsWith("107.77.208.")},this.generateNewRandomIp=function(t){t=t?"107.77.208.":"98.115.221.";return t+=Math.floor(255*Math.random())},this.tryParse=function(t){try{return JSON.parse(t)}catch(t){this.failCount++,this.logger(t)}return null},this.tryParseInt=function(t){try{return parseInt(t,10)}catch(t){return this.failCount++,this.logger(t),-1}},this.getRequest=function(t,e,i){var r=new XMLHttpRequest;this.intentIqConfig.isAsyncServerRequest?r.open("GET",t,!0):r.open("GET",t,!1),r.withCredentials=!0,r.timeout=e,r.onreadystatechange=function(){i&&4===r.readyState&&i(r.responseText)},r.send()},this.createRequestUrl=function(){var t=this.intentIqConfig.iiqServerAddress+"/profiles_engine/ProfilesEngineServlet?at=39&mi=10&dpi=".concat(this.intentIqConfig.partner,"&pt=17&dpn=1");return t+=this.intentIqConfig.pai?"&pai="+encodeURIComponent(this.intentIqConfig.pai):"",t+=this.version?"&jsver="+encodeURIComponent(this.version):"",t=this.appendFirstPartyDataToUrl(t),t+="number"==typeof this.callCount?"&iiqcallcount="+encodeURIComponent(this.callCount):"",t+="number"==typeof this.failCount?"&iiqfailcount="+encodeURIComponent(this.failCount):"",t+="number"==typeof this.noDataCounter?"&iiqnodata="+encodeURIComponent(0<this.noDataCounter):"",t+="&iiqlocalstorageenabled="+encodeURIComponent(this.failCount),t=this.addUniquenessToUrl(t),t=this.addMetaData(t),t+="&cttl="+encodeURIComponent(this.currentDataTTl),t=this.appendPartnersFirsyParty(t),this.appendReferrerToUrl(t)},this.appendReferrerToUrl=function(t){return document.referrer&&""!==document.referrer&&(t+="&vrref="+encodeURIComponent(document.referrer)),t},this.addMetaData=function(t){return isNaN(this.intentIqConfig.sourceMetaData)?t:t+="&fbp="+this.intentIqConfig.sourceMetaData},this.addUniquenessToUrl=function(t){return t+="&tsrnd="+Math.floor(1e3*Math.random())+"_"+(new Date).getTime()},this.appendFirstPartyDataToUrl=function(t){return t+=this.isDebug()?"&ip="+encodeURIComponent(this.generateRandomUsIp()):"",t+=this.firstPartyData.pid?"&pid="+encodeURIComponent(this.firstPartyData.pid):"",t+=this.firstPartyData.dbsaved?"&dbsaved="+encodeURIComponent(this.firstPartyData.dbsaved):"",t+=this.firstPartyData.pcid?"&iiqidtype=2&iiqpcid="+encodeURIComponent(this.firstPartyData.pcid):"",t+=this.firstPartyData.pcidDate?"&iiqpciddate="+encodeURIComponent(this.firstPartyData.pcidDate):""},this.containsEids=function(t){var e=!1;try{e="data"in t&&"eids"in t.data&&Array.isArray(t.data.eids)&&0<t.data.eids.length}catch(t){}return e},this.loadPartnerData=function(){var t=this.tryParse(this.readData(this.PARTNER_DATA_KEY));return t?(t.data&&(this.intentIqData=t.data),"number"==typeof t.callCount&&(this.callCount=t.callCount),"number"==typeof t.failCount&&(this.failCount=t.failCount),"number"==typeof t.noDataCounter&&(this.noDataCounter=t.noDataCounter),t.date&&(this.shouldCallServer=Date.now()-t.date>this.currentDataTTl),"number"==typeof t.cttl&&(this.currentDataTTl=t.cttl,this.logger("Custom data TTL present, value: "+this.currentDataTTl)),this.logger("Data read: "+JSON.stringify(t))):(t={},this.shouldCallServer=!0),t},this.loadOrCreateFirstPartyData=function(){var t=this.tryParse(this.readData(this.FIRST_PARTY_KEY));return t&&t.pcid?t&&!t.pcidDate&&(t.pcidDate=Date.now(),this.storeData(this.FIRST_PARTY_KEY,JSON.stringify(t))):(t={pcid:this.generateGUID(),pcidDate:Date.now()},this.storeData(this.FIRST_PARTY_KEY,JSON.stringify(t))),t},this.makeServerRequest=function(t){var e=this,i=!1;this.logger("Sending request"),this.getRequest(t,this.intentIqConfig.timeoutInMillis,function(t){t=e.tryParse(t);t&&(e.partnerData.date=Date.now(),"pid"in t&&(e.firstPartyData.pid=t.pid,i=!0),"dbsaved"in t&&(e.firstPartyData.dbsaved=t.dbsaved,i=!0),"cttl"in t?(e.currentDataTTl=t.cttl,e.logger("Received custom TTL from server, value: "+e.currentDataTTl)):(e.currentDataTTl=e.intentIqConfig.refreshInMillis,e.logger("No custom TTL from server, setting customers value: "+e.currentDataTTl)),e.clearCounters(),e.containsEids(t)||(e.noDataCounter=e.noDataCounter+1),t.ls&&(e.partnerData.data=t.data,e.intentIqData=t.data),e.logger("Received Data: "+JSON.stringify(t))),i&&e.storeData(e.FIRST_PARTY_KEY,JSON.stringify(e.firstPartyData)),e.savePartnerDataToLocalStore(),e.intentIqConfig.callback&&!e.wasCallbackFired&&(e.intentIqConfig.callback(e.intentIqData),e.wasCallbackFired=!0)})},this.savePartnerDataToLocalStore=function(){this.partnerData||(this.partnerData=this.tryParse(this.readData(this.PARTNER_DATA_KEY))),this.partnerData&&(this.partnerData.callCount=this.callCount,this.partnerData.failCount=this.failCount,this.partnerData.noDataCounter=this.noDataCounter,this.partnerData.cttl=this.currentDataTTl,this.storeData(this.PARTNER_DATA_KEY,JSON.stringify(this.partnerData)))},this.isDefined=function(t){return void 0!==t&&null!=t},this.isEmptyObjAndArray=function(t){return!t||"object"==_typeof(t)&&this.isEmpty(t)},this.isEmpty=function(t){for(var e in t)if(t.hasOwnProperty(e))return!1;return!0},this.appendImage=function(t){var e;this.isDefined(t)&&((e=document.createElement("img")).src=t,e.width=1,e.height=1,document.body.appendChild(e))},this.appendPartnersFirsyParty=function(t){try{if(-1===this.intentIqConfig.partnerClientIdType)return t;""!==this.intentIqConfig.partnerClientId&&(t=(t=t+"&pcid="+this.intentIqConfig.partnerClientId)+"&idtype="+this.intentIqConfig.partnerClientIdType)}catch(t){this.logger(t)}return t},this.createPixelUrl=function(){var t=this.intentIqConfig.iiqPixelServerAddress+"/profiles_engine/ProfilesEngineServlet?at=20&mi=10&secure=1";return t+="&dpi="+this.intentIqConfig.partner,t+="&rnd="+this.getRandom(0,1e6),t=this.appendFirstPartyDataToUrl(t),t=this.addUniquenessToUrl(t),t=this.addMetaData(t),t=this.appendPartnersFirsyParty(t),this.appendReferrerToUrl(t)},this.pixelSync=function(){try{var t,e=this.readData(this.LAST_SYNC_KEY);(!e||Date.now()-e>this.syncRefreshMillis)&&(t=this.createPixelUrl(),this.appendImage(t),this.storeData(this.LAST_SYNC_KEY,Date.now()+""))}catch(t){this.logger("Error adding pixel to DOM "+t)}},this.getRandom=function(t,e){return Math.floor(Math.random()*(e-t+1)+t)},this.getIntentIqData=function(){try{if(this.intentIqConfig.mode===this.pixelOperationalMode)return{};if("number"!=typeof this.intentIqConfig.partner)return this.failCount++,this.logger("intentIqId requires a valid partner to be defined"),{};var t;this.partnerData=this.loadPartnerData(),this.wasCallFromConstructor?(this.callCount++,this.containsEids(this.partnerData)||(this.noDataCounter=this.noDataCounter+1)):this.wasCallFromConstructor=!0,this.shouldCallServer&&!this.wasServerCalled?(this.wasServerCalled=!0,t=this.createRequestUrl(),this.makeServerRequest(t)):this.savePartnerDataToLocalStore();var e=!this.isEmptyObjAndArray(this.intentIqData)||!this.shouldCallServer;return this.intentIqConfig.callback&&!this.wasCallbackFired&&e&&(this.intentIqConfig.callback(this.intentIqData),this.wasCallbackFired=!0),this.intentIqData||{}}catch(t){this.failCount++,this.logger("Exception occurred during main logic: "+t),this.savePartnerDataToLocalStore()}return{}},this.getIntentIqEids=function(){try{if(e.intentIqConfig.mode===e.pixelOperationalMode)return[];var t=e.getIntentIqData();if(t&&t.eids)return t.eids}catch(t){e.failCount++,e.logger("Parse response eids got exception"),e.savePartnerDataToLocalStore()}return[]},this.enrichRequests=function(e){var i=this;try{if(this.intentIqConfig.mode===this.pixelOperationalMode)return e;if(!this.intentIqConfig.enhanceRequests)return this.logger("Enrichment is turned off"),e;var t=JSON.parse(JSON.stringify(e));return this.logger("======Staring eids enrichment==="),Array.isArray(t)?t.forEach(function(t){i.requestEnhancer(t)}):"method"in t&&this.requestEnhancer(t),this.logger(t),this.logger("======Done eids enrichment==="),t}catch(t){return this.logger(t),e}},this.requestEnhancer=function(t){var e=this,i=[];try{i=this.getIntentIqEids()}catch(t){return void this.logger(t)}if(!i||!Array.isArray(i)||0==i.length){if(this.logger("Failed to get EIDS"),!this.isDebug())return;i=[{source:"domain.com",uids:[{id:"value read from cookie or local storage",atype:1,ext:{stype:"ppuid"}}]},{source:"3rdpartyprovided.com",uids:[{id:"value read from cookie or local storage",atype:3,ext:{type:"sha256email"}}]}]}this.logger("Adding eids - "+i);try{if(!("method"in t))return void this.logger("Method field not found. Unsupported format. ")}catch(t){return void this.logger(t)}switch(t.method){case"POST":this.logger("Enriching POST"),this.logger("Updating data filed");try{var r=t.data,a="";t.data.startsWith('"')&&(r=r.substring(1,r.length-1),a='"');var n=JSON.parse(r);"user"in n||(n.user={eids:[]}),"eids"in n.user||(n.user.eids=[]),n.user.eids=n.user.eids.concat(i),t.data=a+JSON.stringify(n)+a}catch(t){this.logger(t)}this.logger("Updating request data.");try{if(!("bidderRequest"in t))return void this.logger("Failed to find bidderRequest")}catch(t){return void this.logger(t)}try{"bids"in t.bidderRequest&&Array.isArray(t.bidderRequest.bids)&&t.bidderRequest.bids.forEach(function(t){"userId"in t?"pubProvidedId"in t.userId?Array.isArray(t.userId.pubProvidedId)&&(t.userId.pubProvidedId=t.userId.pubProvidedId.concat(i)):t.userId.pubProvidedId=i:(e.logger("UserIds not found - adding"),t.userId={},t.userId.pubProvidedId=i),"userIdAsEids"in t?Array.isArray(t.userIdAsEids)&&(t.userIdAsEids=t.userIdAsEids.concat(i)):(e.logger("userIdAsEids not found - adding"),t.userIdAsEids=i)})}catch(t){return void this.logger(t)}break;case"GET":this.logger("GET request skipped - currently not supported")}this.logger("Enchanted request: "+t.bidderRequest)},this.firstPartyData=this.loadOrCreateFirstPartyData(),this.getIntentIqData(),this.intentIqConfig.mode!==this.vrOperationalMode&&this.pixelSync()}